<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>组织管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
    <link rel="stylesheet" href="${ctx}/extends/zTree_v3/css/zTreeStyle/zTreeStyle.css"/>
    <link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
    <link rel="stylesheet" href="${ctx}/css/public.css">
    <script type="text/javascript">
        var __ctx = "${ctx}";
    </script>
</head>
<body style="overflow: auto;">
    <div class="layui-row layui-col-space15" style="margin: 0">
        <div class="layui-col-md12" style="padding: 0">
            <div class="layui-card">
                <div class="layui-card-body" style="padding: 10px;padding-top: 0">
                    <script type="text/html" id="orgToolbar">
                        <div class="layui-btn-group">
                            <button class="layui-btn layui-btn-sm" id="btn-expand"><i
                                    class="layui-icon layui-icon-down"></i> 全部展开
                            </button>
                            <button class="layui-btn layui-btn-sm" id="btn-fold"><i class="layui-icon layui-icon-left"></i>
                                全部折叠
                            </button>
                            <button class="layui-btn layui-btn-sm" id="btn-refresh">
                                <i class="layui-icon layui-icon-loading"></i> 刷新表格
                            </button>
                        </div>
                    </script>
                    <table id="orgSysTable" class="layui-table" lay-filter="orgSysTable"></table>
                </div>
            </div>
        </div>
    </div>

    <script type="text/html" id="orgRightToolsBar">
        {{# if(d.isTenant === 1){ }}
        <a class="layui-btn layui-btn-xs" lay-event="allocaRes">分配资源</a>
        {{# }}}
        <a class="layui-btn layui-btn-xs" lay-event="addOrg">增加组织</a>
        <a class="layui-btn layui-btn-xs" lay-event="editOrg">编辑</a>
        {{# if(d.isParentBoolean === false){ }}
        <a class="layui-btn layui-btn-xs" lay-event="sort">排序</a>
        {{# } }}
        {{# if(d.orgId != layui.setter.dimensionVariable){ }}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
        {{# } }}
    </script>
    <div class="resAllocation" hidden style="display: none">
        <ul id="resAllocations" class="ztree" style="background: #fff;height: 435px;overflow: auto"></ul>
        <div style="margin: 9px;text-align: center">
            <a class="layui-btn saveData">保存</a>
            <a class="layui-btn advancedSetting">高级设置</a>
            <a class="layui-btn cancelData layui-btn-danger" >取消</a>
        </div>
    </div>
    <div class="resAdvSet" hidden style="display: none">
        <ul id="advSettings" class="ztree" style="background: #fff;height: 446px;overflow: auto"></ul>
        <div style="margin: 10px;text-align: center">
            <a class="layui-btn saveOrgAllocation">保存</a>
            <a class="layui-btn cenPubData layui-btn-danger">取消</a>
        </div>
    </div>
    <div class="importResources" hidden style="display: none">
        <form class="layui-form">
            <a class="layui-btn " lay-submit lay-filter="resImportForms" style="margin: 10px" id="resImportForms">导入</a>
            <a class="layui-btn " style="margin: 10px">导出模板</a>
            <div class="layui-form-item">
                <label class="layui-form-label">选择文件:</label>
                <div class="layui-input-block">
                    <input type="file" id="importResourcesTest">
                </div>
            </div>
        </form>
    </div>
    <script src="${ctx}/extends/jquery.min.js" type="text/javascript"></script>
    <script src="${ctx}/public/component.js" type="text/javascript"></script>
    <script src="${ctx}/layui/layui.js"></script>
    <script src="${ctx}/extends/zTree_v3/js/jquery.ztree.all.min.js" type="text/javascript"></script>
    <script src="${ctx}/extends/zTree_v3/js/jquery.ztree.exhide.min.js" type="text/javascript"></script>
    <script>
        layui.config({
            base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
        }).extend({
            index: 'index' //主入口模块
        }).use(['index', 'setter', 'commons', 'form', 'jquery', 'layer', 'treetable', 'table', 'upload'], function () {
            var setter = layui.setter,
                $ = layui.$,
                table = layui.table,
                commons = layui.commons,
                form = layui.form,
                upload = layui.upload,
                treetable = layui.treetable,
                advPublicResId=[],

                renderTable = function () {
                    treetable.render({
                        treeColIndex: 0,//树形图标显示在第几列
                        treeSpid: setter.LStorage.get('orgId'),//最上级的父级id
                        treeIdName: 'orgId',//id字段的名称
                        treePidName: 'pid',//pid字段的名称
                        defaultToolbar: ['filter', 'print'],
                        treeDefaultClose: false,//是否默认折叠
                        treeLinkage: true,//父级展开时是否自动展开所有子级
                        elem: '#orgSysTable',
                        url: serverPath.systemPath + setter.sysLinkPath + "sysOrg/listFlat",
                        toolbar: '#orgToolbar',
                        page: false,
                        icon_key: 'orgName',
                        is_checkbox: true,
                        checked: {
                            key: 'orgId',
                            data: []
                        },
                        cols: [[
                            {field: 'name', minWidth: 100, title: '名称'},
                            {
                                minWidth: 300,
                                title: '操作',
                                toolbar: '#orgRightToolsBar'
                            }
                        ]],
                        done: function () {
                            layer.closeAll('loading');
                        }
                    });
                };
            renderTable();
            // $('body > div.resAllocation ').remove();
            // $('body').append($('div.resAllocation'));
            // $('body > .importResources').remove();
            // $('body').append($('.importResources'));
            // $('body > .resAdvSet').remove();
            // $('body').append($('.resAdvSet'));

            //全部展开
            $(document).on("click", "#btn-expand", function () {
                treetable.expandAll('#orgSysTable');
            });

            //全部折叠
            $(document).on("click", "#btn-fold", function () {
                treetable.foldAll('#orgSysTable');
            });

            //刷新表格
            $(document).on("click", "#btn-refresh", function () {
                renderTable();
            });

            table.on('tool(orgSysTable)', function (obj) {
                switch (obj.event) {
                    case 'allocaRes':
                        layer.open({
                            type: 1,
                            area: ['360px', '550px'],
                            title: ['分配资源'],
                            content: $(".resAllocation"),
                            success: function (lay, index) {
                                $.ajax({
                                    url: serverPath.systemPath + setter.sysLinkPath + "sysRes/listResForOrgAssignRes",
                                    type: "get",
                                    dataType: 'json',
                                    data: {
                                        orgId: obj.data.orgId,
                                        type: "0"
                                    },
                                    success: function (data) {
                                        $.fn.zTree.init($('#resAllocations'), {
                                            check: {
                                                enable: true
                                            },
                                            data: {
                                                simpleData: {
                                                    enable: true,
                                                    idKey: "resId",
                                                    pIdKey: "parentId"
                                                }
                                            }
                                        }, data.data);
                                    },
                                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                                        layer.alert("暂无数据，请稍后操作！" + XMLHttpRequest.status);
                                    }
                                });
                                $.ajax({
                                    url: serverPath.systemPath + setter.sysLinkPath + "sysRes/listResForOrgAssignRes",
                                    type: "get",
                                    contentType: 'application/json',
                                    dataType: 'json',
                                    data: {
                                        orgId: obj.data.orgId,
                                        type: "1"
                                    },
                                    success: function (data) {
                                        var zNodes = data.data;
                                        var setting = {
                                            check: {
                                                enable: true
                                            },
                                            callback: {
                                                onAsyncSuccess: zTreeOnAsyncSuc
                                            },
                                            data: {
                                                simpleData: {
                                                    enable: true,
                                                    idKey: "resId",
                                                    pIdKey: "parentId"
                                                }
                                            }
                                        };
                                        $.fn.zTree.init($('#advSettings'), setting, zNodes);
                                    },
                                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                                        layer.alert("暂无数据，请稍后操作！" + XMLHttpRequest.status);
                                    },
                                });
                                function zTreeOnAsyncSuc() {
                                    advPublicResId = [];
                                    var treeObj = $.fn.zTree.getZTreeObj("advSettings");
                                    var nodes = treeObj.getCheckedNodes(true);
                                    $.each(nodes, function (i, item) {
                                        if (item.resId !== "0") {
                                            advPublicResId.push(item.resId);
                                        }
                                    });
                                    return false
                                }

                                //高级设置
                                $('.resAllocation .advancedSetting').unbind("click");
                                $(".resAllocation .advancedSetting").bind('click', function () {
                                    var indexAdv = layer.open({
                                        type: 1,
                                        area: ['380px', '560px'],
                                        title: ['公用资源分配'],
                                        content: $(".resAdvSet")
                                    });
                                    $(".resAdvSet .saveOrgAllocation").click(function () {
                                        zTreeOnAsyncSuc();
                                        layer.close(indexAdv);
                                    });
                                    $(".resAdvSet .cenPubData").click(function () {
                                        zTreeOnAsyncSuc();
                                        layer.close(indexAdv);
                                    });
                                    return false
                                });

                                $('.resAllocation .saveData').unbind("click");
                                $('.resAllocation .saveData').bind("click", function () {
                                    if(advPublicResId.length<1){
                                        zTreeOnAsyncSuc()
                                    }
                                    var treeObj = $.fn.zTree.getZTreeObj("resAllocations");
                                    var nodes = treeObj.getCheckedNodes(true);
                                    var arr = [];
                                    $.each(nodes, function (i, item) {
                                        if (item.resId !== "0") {
                                            arr.push(item.resId);
                                        }
                                    });
                                    var resIds = advPublicResId.concat(arr);
                                    $.ajax({
                                        url: serverPath.systemPath + setter.sysLinkPath + "sysOrg/assign",
                                        type: "post",
                                        dataType: 'json',
                                        data: {
                                            orgId: obj.data.orgId,
                                            resIds: resIds.join(",")
                                        },
                                        success: function (data) {
                                            if (commons._filterRecord(data)) {
                                                layer.alert(data.msg, function () {
                                                    layer.closeAll();
                                                });
                                            } else {
                                                layer.alert(data.msg);
                                            }
                                        },
                                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                                            layer.alert("暂无数据，请稍后操作！" + XMLHttpRequest.status);
                                        },
                                    });
                                });
                                $('.resAllocation .cancelData').unbind("click");
                                $('.resAllocation .cancelData').bind("click", function () {
                                    layer.close(index);
                                })
                            }
                        });
                        break;
                    case 'addOrg' :
                        layer.open({
                            type: 2,
                            area: ['530px', '592px'],
                            title: ['增加组织'],
                            content: serverPath.systemPath +  setter.sysLinkPath + 'sysOrg/view/update?pid=' + obj.data.orgId +"&isTenant="+obj.data.isTenant
                        });
                        break;

                    case 'editOrg' :
                        layer.open({
                            type: 2,
                            area: ['530px', '592px'],
                            title: ['编辑组织'],
                            content: serverPath.systemPath + setter.sysLinkPath + 'sysOrg/view/update?orgId=' + obj.data.orgId +"&isTenant="+obj.data.isTenant
                        });
                        break;

                    case 'delete':
                        layer.confirm('确定删除当前数据吗？', function () {
                            $.ajax({
                                url: serverPath.systemPath + setter.sysLinkPath + "sysOrg/del",
                                type: "POST",
                                dataType: 'json',
                                data: {
                                    orgId: obj.data.orgId,
                                },
                                success: function (data) {
                                    if (commons._filterRecord(data)) {
                                        layer.alert(data.msg, function (ind) {
                                            layer.close(ind);
                                            renderTable();
                                        });
                                    } else {
                                        layer.alert(data.msg);
                                    }
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    layer.alert("操作异常，请稍后操作！" + XMLHttpRequest.status);
                                },
                            });
                        })
                        return false;
                        break;

                    case 'sort':
                        layer.open({
                            type: 2,
                            area: ['450px', '477px'],
                            title: ['排序'],
                            content: serverPath.systemPath + setter.sysLinkPath + 'sysOrg/view/sort?orgId=' + obj.data.orgId
                        });
                        break;
                }
            })
        });
    </script>
</body>
</html>
<link rel="stylesheet" href="${ctx}/css/font/iconfontie.css" media="all"/>
<link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>